<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goods.categories">
      <!-- 面包屑 -->
      <XtxBread separator=">">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <GoodsImage :goodsList="goods.mainPictures" />
          <GoodsSales :good="goods" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodsName :good="goods" />
          <GoodsSku :goods="goods" @change="skuChange" />
          <XtxNumbox v-model="count" />
          <XtxButton size="middle" type="primary" @click="shopPing"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodHot v-for="itm in 3" :key="itm" :type="itm"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import { ref, onMounted, provide } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsSku from './components/Sku'
import GoodsTabs from './components/goods-tabs'
import GoodHot from './components/good-hot'
import { useStore } from 'vuex'
import { Message } from '@/components/Message'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodHot
  },
  setup () {
    const goods = ref({})
    provide('goods', goods)
    const route = useRoute()
    const count = ref(2)
    const skuId = ref(null)
    function skuChange (sku) {
      if (sku.skuId) {
        skuId.value = sku.skuId
      }
    }
    async function goodsObj () {
      const res = await findGoods(route.params.id)
      console.log(res.result)
      goods.value = res.result
    }
    const store = useStore()
    function shopPing () {
      store.dispatch('cart/asyncInsertCart', { skuId: skuId.value, count: count.value }).then(() => {
        Message({ type: 'success', text: '添加购物车成功!' })
      })
    }
    onMounted(() => {
      goodsObj()
    })
    return { goods, count, skuChange, shopPing }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
